<?php

class Sync extends Controller
{
	function __construct()
	{			
		parent::Controller();
		if($this->input->get('debug'))
			$this->output->enable_profiler(true);			
	}


	function recommendlist($pageno=0)
	{
		$url = "http://download.csdn.net/index.php/rest/source/getRecommendList/$pageno?";
		$ret = json_decode(file_get_contents($url),true);
		$slist = $ret['slist'];
		$this->list2db($slist);
		
	}
	
	function lastlist($pageno=1)
	{
		$url = "http://u.download.csdn.net/index.php/rest/source/getLastList/$pageno/20";
		$ret = json_decode(file_get_contents($url),true);
		$slist = $ret['slist'];
		$this->list2db($slist);
	}
	
	
	function list2db($slist)
	{
		$this->load->library("sources_lib");
		foreach($slist as $row)
		{
			$source = new stdClass();
			if($row['id']==0) continue; 
			$source->realid = $row['id'];
			$source->title = $row['title'];
			$source->tag = implode(" ",$row['tag']);
			$source->categoryid = $row['categoryid'];
			$source->username = $row['username'];
			$source->sourcesize = $row['sourcesize'];
			$source->pubdate = $row['pubdate'];
			$source->description = $row['description'];
			$source->status = $row['status'];
			$source->filetype = $row['filetype'];
			$source->originfile = $row['originfile'];
			$source->fileaddr = $row['fileaddr'];
			$source->clicknum = $row['clicknum'];
			$source->fileaddr_prefix = $row['fileaddr_prefix'];
			$source->md5 = $row['md5'];
			$source->isrepeat = $row['isrepeat'];
			$ret = $this->sources_lib->add($source);
			if(is_object($ret))
			{
				echo $ret->id ,"<br/>";
			}else
			{
				echo $ret ,"<br/>";
			}
		}				
	}
	
	function info($id)
	{
		$url = "http://download.csdn.net/index.php/rest/source/getSourceInfo/$id";
		$info = json_decode(file_get_contents($url));
		print_r($info);
	}
	

	function taglist($tag,$pageno=0)
	{
		$tag = trim($tag);
		if($tag=="") return ;
		$url = "http://download.csdn.net/index.php/rest/source/getListByTag/$tag/$pageno";
		$info = json_decode(file_get_contents($url));
		print_r($info);		
	}
		
	function user_upload_list($username,$pageno=1)
	{
		$username = trim($username);
		if($username=="") return ;
		if($pageno==1)
			$url = "http://download.csdn.net/index.php/rest/source/getUploadListByUsername/$username";
		else
			$url = "http://download.csdn.net/index.php/rest/source/getUploadListByUsername/$username/$pageno";
		$info = json_decode(file_get_contents($url));
		print_r($info);				
	}	
}

